package com.angle.im.server.config.mq;

import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author zhangwei
 * @date 2018/9/25 14:59
 * @desc MQ生产者配置信息
 */
@Configuration
@Slf4j
public class ProducerConfiguration {

    private final String ACCESS_KEY = "your ons access key";
    private final String SECRET_KEY = "your ons secret key";
    private final String SEND_TO_ONE = "GID_SEND_TO_ONE";
    private final String ONS_ADDR = "http://onsaddr.mq-internet-access.mq-internet.aliyuncs.com:80";

    @Bean
    public Producer broadCastClient() {
        Properties properties = new Properties();
        // 阿里云身份验证，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.AccessKey, ACCESS_KEY);
        properties.put(PropertyKeyConst.SecretKey, SECRET_KEY);
        properties.put(PropertyKeyConst.GROUP_ID, SEND_TO_ONE);
        //设置发送超时时间，单位毫秒
        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
        // 设置 TCP 接入域名（此处以公共云生产环境为例）
        properties.put(PropertyKeyConst.NAMESRV_ADDR, ONS_ADDR);
        Producer producer = ONSFactory.createProducer(properties);
        // 在发送消息前，必须调用 start 方法来启动 Producer，只需调用一次即可
        producer.start();
        // 在应用退出前，销毁 Producer 对象   producer.shutdown();
        // 注意：如果不销毁也没有问题
        log.info("Producer Started");
        return producer;
    }

}
